﻿using System.Collections.Generic;
using Navigant.DataContracts;
using Navigant.Infrastructure.Cases;
using Navigant.Infrastructure.ClientPortals;
using Navigant.Model.Cases;
using Navigant.Model.ClientPortals;
using Navigant.Model.Custodians;
using Navigant.Model.PreservationNotices;

namespace Navigant.Mapping
{
    public static class Converter
    {
        #region ToCaseContract

        public static CaseContract ToCaseContract(ICase caseSite)
        {
            CaseContract contract = null;
            if (caseSite != null)
            {
                contract = new CaseContract();
                contract.ClientPortalId = caseSite.ClientPortalId;
                contract.Id = caseSite.Id;
                contract.Name = caseSite.Name;
                contract.Url = caseSite.SiteAddress.AbsoluteUri;
            }
            return contract;
        }

        #endregion

        #region ToCase

        public static ICase ToCase(CaseContract contract)
        {
            return CaseService.GetCaseBy(contract.ClientPortalId, contract.Id);
        }

        #endregion

        #region ToPortalContract

        public static PortalContract ToPortalContract(IClientPortal portal)
        {
            PortalContract contract = null;
            if (portal != null)
            {
                contract = new PortalContract();
                contract.Id = portal.Id;
                contract.Name = portal.Name;
                contract.ConfigurationSettings = new Dictionary<string, string>(portal.ConfigurationSettings);
            }
            return contract;
        }

        #endregion

        #region ToClientPortal

        public static IClientPortal ToClientPortal(PortalContract contract)
        {
            return ClientPortalService.GetClientPortalBy(contract.Id);
        }

        #endregion

        #region ToCustodianContract

        public static CustodianContract ToCustodianContract(Custodian custodian)
        {
            CustodianContract contract = null;
            if (custodian != null)
            {
                contract = new CustodianContract();
                contract.Email = custodian.Email;
                contract.ExtendedProperties = new Dictionary<string, string>(custodian.ExtendedProperties);
                contract.FirstName = custodian.Name.FirstName;
                contract.IsInternal = custodian.IsInternal;
                contract.IsPerson = custodian.IsPerson;
                contract.Key = custodian.Id;
                contract.LastName = custodian.Name.LastName;
                contract.MiddleName = custodian.Name.MiddleName;
                contract.ParentCase = Converter.ToCaseContract(custodian.ParentCase);
                contract.Status = custodian.Status.ToString();
            }
            return contract;
        }

        #endregion

        #region ToCustodian

        public static Custodian ToCustodian(CustodianContract contract)
        {
            Custodian custodian = null;
            if (contract != null)
            {
                ICase parentCase = Converter.ToCase(contract.ParentCase);
                CustodianName name = new CustodianName(contract.FirstName, 
                    contract.MiddleName, contract.LastName);
                custodian = new Custodian(parentCase, name, contract.Email, contract.Key);
                custodian.IsInternal = contract.IsInternal;
                custodian.IsPerson = contract.IsPerson;
                foreach (string key in contract.ExtendedProperties.Keys)
                {
                    custodian.SetExtendedProperty(key, contract.ExtendedProperties[key]);
                }
            }
            return custodian;
        }

        #endregion

        #region ToPreservationNoticeTemplateContract

        public static PreservationNoticeTemplateContract ToPreservationNoticeTemplateContract(PreservationNoticeTemplate template)
        {
            PreservationNoticeTemplateContract contract = null;
            if (template != null)
            {
                contract = new PreservationNoticeTemplateContract();
                contract.ExtendedProperties = new Dictionary<string, string>(template.ExtendedProperties);
                contract.Body = template.Body;
                contract.Subject = template.Subject;
                contract.Name = template.Name;
                contract.Key = template.Id;
                contract.ParentCase = Converter.ToCaseContract(template.ParentCase);
            }
            return contract;
        }

        #endregion

        #region ToPreservationNoticeTemplate

        public static PreservationNoticeTemplate ToPreservationNoticeTemplate(PreservationNoticeTemplateContract contract)
        {
            PreservationNoticeTemplate template = null;
            if (contract != null)
            {
                ICase parentCase = Converter.ToCase(contract.ParentCase);
                template = new PreservationNoticeTemplate(contract.Key, parentCase, 
                    contract.Name, contract.Subject, contract.Body);
                foreach (string key in contract.ExtendedProperties.Keys)
                {
                    template.SetExtendedProperty(key, contract.ExtendedProperties[key]);
                }
            }
            return template;
        }

        #endregion

        #region ToPreservationNoticeContract

        public static PreservationNoticeContract ToPreservationNoticeContract(PreservationNotice notice)
        {
            PreservationNoticeContract contract = null;
            if (notice != null)
            {
                contract = new PreservationNoticeContract();
                contract.ExtendedProperties = new Dictionary<string, string>(notice.ExtendedProperties);
                if (notice.To != null)
                {
                    contract.CustodianId = notice.To.Id;
                    contract.CustodianName = notice.To.Name.FullName;
                    contract.CustodianEmail = notice.To.Email;
                }
                contract.DateMessageAcknowledged = notice.DateMessageAcknowledged;
                contract.DateMessageCreated = notice.DateMessageCreated;
                contract.DateMessageSent = notice.DateMessageSent;
                contract.FromAddress = notice.FromAddress;
                contract.Status = notice.Status.ToString();
                contract.TemplateId = notice.TemplateId;
                contract.TemplateName = notice.TemplateName;
                if (notice.Template != null)
                {
                    contract.TemplateSubject = notice.Template.Subject;
                }
                contract.Key = notice.Id;
                contract.ParentCase = Converter.ToCaseContract(notice.ParentCase);
            }
            return contract;
        }

        #endregion
    }
}